/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import javax.media.opengl.GL;

public class Alphabet {

    private GLUT glut = new GLUT();

    private float[] letterX1;
    private float[] letterY1;
    private float[] letterX2;
    private float[] letterY2;
    private boolean[] letterOver;
    private boolean[] letterClicked;

    private float WIDTH = 750f;
    private float HEIGHT = 20f;

    private float startX;
    private float startY;

    public Alphabet() {
        letterX1 = new float[26];
        letterY1 = new float[26];
        letterX2 = new float[26];
        letterY2 = new float[26];
        letterOver = new boolean[26];
        letterClicked = new boolean[26];

        for(int i=0; i<26; i++) {
            letterX1[i] = i*28;
            letterY1[i] = 0f;
            letterX2[i] = letterX1[i] + 30f;
            letterY2[i] = 20f;
            letterOver[i] = false;
            letterClicked[i] = false;
        }

        letterClicked[0] = true;
    }

    public void draw(GL gl, float x, float y) {

        startX = x;
        startY = y;

        for(int i=0; i<26; i++) {

            gl.glPushMatrix();
                gl.glBegin(GL.GL_QUADS);
                setBoxColor1(gl,i);
                gl.glVertex2f(letterX1[i], letterY1[i]);
                setBoxColor1(gl,i);
                gl.glVertex2f(letterX1[i], letterY2[i]);
                setBoxColor2(gl,i);
                gl.glVertex2f(letterX2[i], letterY2[i]);
                setBoxColor2(gl,i);
                gl.glVertex2f(letterX2[i], letterY1[i]);
                gl.glEnd();
            gl.glPopMatrix();

            gl.glPushMatrix();
                gl.glColor3f(1f,1f,1f);
                gl.glRasterPos2f(letterX1[i]+5f,letterY1[i]+5f);
                glut.glutBitmapString(GLUT.BITMAP_9_BY_15, getLetter(i));
            gl.glPopMatrix();
        }
    }

    private String getLetter(int pos) {
        switch(pos) {
            case  0: return "A";
            case  1: return "B";
            case  2: return "C";
            case  3: return "D";
            case  4: return "E";
            case  5: return "F";
            case  6: return "G";
            case  7: return "H";
            case  8: return "I";
            case  9: return "J";
            case 10: return "K";
            case 11: return "L";
            case 12: return "M";
            case 13: return "N";
            case 14: return "O";
            case 15: return "P";
            case 16: return "Q";
            case 17: return "R";
            case 18: return "S";
            case 19: return "T";
            case 20: return "U";
            case 21: return "V";
            case 22: return "W";
            case 23: return "X";
            case 24: return "Y";
            case 25: return "Z";
            default: return "";
        }
    }

    private void setBoxColor1(GL gl, int index) {
        if( letterClicked[index] )
        {
            gl.glColor3f(0f,0f,1f);
        }
        else if( letterOver[index] )
        {
            gl.glColor3f(1f,0f,0f);
        }
        else
        {
            gl.glColor3f(.7f,.7f,.7f);
        }
    }

    private void setBoxColor2(GL gl, int index) {
            gl.glColor3f(0f,0f,0f);
    }

    public void mouseMoved(int x, int y) {

        if( x >= startX &&
            x <= (startX + WIDTH) &&
            y >= startY &&
            y <= (startY + HEIGHT) )
        {
            float mouseX = x - startX;
            float mouseY = y - startY;

            for (int i=0; i<26; i++)
            {
                if ( ( mouseX >= letterX1[i] ) &&
                     ( mouseX <  letterX2[i]-2 ) &&
                     ( mouseY >= letterY1[i] ) &&
                     ( mouseY <  letterY2[i]-2 ) )
                {
                    letterOver[i] = true;
                }
                else
                {
                    letterOver[i] = false;
                }

            }
        }
        else
        {
            for (int i=0; i<26; i++)
            {
                letterOver[i] = false;
            }
        }
    }

    public void mouseReleased(int x, int y) {

        if( x >= startX &&
            x <= (startX + WIDTH) &&
            y >= startY &&
            y <= (startY + HEIGHT) )
        {
            float mouseX = x - startX;
            float mouseY = y - startY;

            for (int i=0; i<26; i++)
            {
                if ( ( mouseX >= letterX1[i] ) &&
                     ( mouseX <  letterX2[i]-2 ) &&
                     ( mouseY >= letterY1[i] ) &&
                     ( mouseY <  letterY2[i]-2 ) )
                {
                    letterClicked[i] = true;
                }
                else
                {
                    letterClicked[i] = false;
                }

            }
        }
    }

    public String getSelectedLetter() {
        for (int i=0; i<26; i++)
        {
            if ( letterClicked[i] ) {
                return getLetter(i);
            }
        }
        return "";
    }
}